using System;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.ComponentModel.Design;
using Widgetsphere.Generator.Models;

namespace Widgetsphere.Generator.Design.Converters
{
	internal class TablePrimaryKeyConverter : TypeConverter
	{
		public TablePrimaryKeyConverter()
		{
		}

		public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
		{
			if (destinationType == typeof(string)) return true;
			return base.CanConvertTo (context, destinationType);
		}

    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    {
      try
      {
        if(destinationType == typeof(string))
        {
          Table table = (Table)context.Instance;
          ModelRoot root = (ModelRoot)table.Root;

          string retval = "";
          foreach(Column column in table.PrimaryKeyColumns)
          {
            retval += column.Name + ", ";
          }
          if(retval != "")
            retval = retval.Substring(0, retval.Length - 2);

          return retval;
        }
      }
      catch(Exception ex) { }
      return null;
    }

		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
		{
			return true;
			//return base.CanConvertFrom (context, sourceType);
		}


	}
}
